package com.sip.stream.utils.zfy.encoder;

import android.media.MediaCodec;
import android.util.Log;

import com.sip.stream.utils.zfy.GSMediaSampleData;
import com.sip.stream.utils.zfy.consumer.IGSMediaObserver;
import com.sip.stream.utils.zfy.consumer.IGSVideoEncoderConsumer;

import java.nio.ByteBuffer;

public class GSFakeVideoEncoder implements IGSVideoEncoder {
    private IGSVideoEncoderConsumer lisener;
    private IGSMediaObserver<IGSNV12Consumer> observer;
    private boolean stopFlag = false;

    @Override
    public void setForcedInterval(int interval) {
    }

    @Override
    public void setPerformanceLevel(int level) {
    }

    @Override
    public void startEncode() {
        this.stopFlag = false;
    }

    @Override
    public void onNV12Stream(byte[] bArr, long j) {
        if (this.lisener != null) {
            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
            bufferInfo.set(0, bArr.length, j, 8);
            ByteBuffer allocate = ByteBuffer.allocate(bArr.length);
            allocate.clear();
            allocate.get(bArr, 0, bArr.length);
            this.lisener.encodeVideoSuccess(GSMediaSampleData.createVideoSampleData(allocate, bufferInfo));
        }
    }

    @Override
    public void addObserver(IGSMediaObserver<IGSNV12Consumer> iGSMediaObserver) {
        this.observer = iGSMediaObserver;
    }

    @Override
    public void addLisener(IGSVideoEncoderConsumer iGSVideoEncoderConsumer) {
        this.lisener = iGSVideoEncoderConsumer;
    }

    @Override
    public void removeLisener(IGSVideoEncoderConsumer iGSVideoEncoderConsumer) {
        this.lisener = null;
        this.stopFlag = true;
        IGSMediaObserver<IGSNV12Consumer> iGSMediaObserver = this.observer;
        if (iGSMediaObserver != null) {
            iGSMediaObserver.onStop(this);
            this.observer = null;
        }
    }

    @Override
    public boolean isStop() {
        return this.stopFlag;
    }
}
